package com.yn.common.request.zy;

import com.yn.common.constants.Constants;
import com.yn.common.model.zy.SubscribeInfoDO;
import com.yn.common.utils.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 机构预约请求DTO
 */
@Data
@ApiModel("机构预约请求")
public class InstAppointmentRequest {

    @ApiModelProperty(value = "机构id ")
    private Integer organId;

    //业务类型(0:普通预约 1:急诊预约 2:机构预约)
    @ApiModelProperty(value = "业务类型")
    @NotBlank(message = "业务类型不能为空")
    private Integer businessType;

    @NotBlank(message = "患者姓名不能为空")
    @ApiModelProperty(value = "患者姓名", required = true, example = "张三")
    private String userOrderName;

    @NotBlank(message = "联系电话不能为空")
    @ApiModelProperty(value = "家属/联系人电话", required = true, example = "13800138000")
    private String userOrderPhone;

    @ApiModelProperty(value = "患者年龄", example = "65")
    private Integer age;

    @ApiModelProperty(value = "性别")
    private Integer gender;

    @ApiModelProperty(value = "医保类型:字典")
    private String medicalInsuranceType;

    @ApiModelProperty(value = "生活能力: 0-自理 1-半自理 2-不能自理", example = "0")
    private Integer livingAbility;

    @ApiModelProperty(value = "接送地址", example = "北京市朝阳区XX街道")
    private String transferAddress;

    @ApiModelProperty(value = "与患者关系:字典")
    private String relationType;

    @ApiModelProperty(value = "健康状况简述")
    private String healthStatus;



    @NotNull(message = "预计出院时间不能为空")
    @ApiModelProperty(value = "预计出院日期", required = true, example = "2025-08-15")
    private String estDischargeTime;

    // 备注信息
    @ApiModelProperty(value = "备注信息")
    private String remark;

    public static SubscribeInfoDO transform(InstAppointmentRequest request) {
        SubscribeInfoDO infoDO = new SubscribeInfoDO();
        infoDO.setBusinessId(request.getOrganId());
        infoDO.setBusinessType(request.getBusinessType());

        infoDO.setUserOrderName(request.getUserOrderName());
        infoDO.setUserOrderPhone(request.getUserOrderPhone());

        if (request.getAge()!=null){
            infoDO.setAge(request.getAge());
        }
        if (request.getGender()!=null){
            infoDO.setGender(request.getGender());
        }
        if(request.getMedicalInsuranceType()!=null){
            infoDO.setMedicalInsuranceType(request.getMedicalInsuranceType());
        }
        if(request.getLivingAbility()!=null){
            infoDO.setLivingAbility(request.getLivingAbility());
        }
        if(request.getTransferAddress()!=null){
            infoDO.setTransferAddress(request.getTransferAddress());
        }
        if(request.getRelationType()!=null){
            infoDO.setRelationType(request.getRelationType());
        }
        if(request.getHealthStatus()!=null){
            infoDO.setHealthStatus(request.getHealthStatus());
        }
        if(request.getEstDischargeTime()!=null){
            Date date = DateUtil.strToDate(request.getEstDischargeTime(), Constants.DATE_FORMAT);
            if (date != null && date.getTime() < System.currentTimeMillis()) {
                throw new RuntimeException("预计出院时间不能早于当前时间");
            }
            infoDO.setEstDischargeTime(date);
        }
        if(request.getRemark() != null){
            infoDO.setRemark(request.getRemark());
        }
        return infoDO;
    }
}